let str = 'cbacdcbc';

let f = str => {
  let stack = [];
  for (let i = 0; i < str.length; i++) {
    let ch = str[i];
    if (stack.includes(ch)) continue;
    while (stack.length && stack[stack.length - 1] > ch && str.indexOf(stack[stack.length - 1], i) !== - 1) {
      stack.pop();
    }
    stack.push(ch);
  }
  return stack.join('');
}

console.log(f(str))